<!-- 物资申领 -->
<template>
  <view class="">
    <view style="width: 100%; margin-top: 20rpx;" v-for="(item,index) in baseList" :key="index">
      <view class="common-body-ex">
        <view class="common-list-ex-patch" v-if="type==='3'">

          <label class="common-list-title-ex">单项工程编码 :</label>

          <text class="common-list-text-ex-patch" style="height: auto;" type="text" :selectable="true">
            {{ item.prjcode }}
          </text>

        </view>
        <view class="common-list-ex-patch" v-if="type!=='1'">

          <label class="common-list-title-ex">站址编码 :</label>

          <text class="common-list-text-ex-patch" style="height: auto;" type="text" :selectable="true">
            {{ item.siteId }}
          </text>

        </view>
        <view class="common-list-ex-patch">

          <label class="common-list-title-ex">物料编码 :</label>

          <text class="common-list-text-ex-patch" style="height: auto;" type="text" :selectable="true">
            {{ type === '3' ? item.categorycode : item.categoryCode }}
          </text>

        </view>
        <view class="common-list-ex-patch" style="height: auto">

          <label class="common-list-title-ex">物料名称 :</label>


          <text class="common-list-text-ex-patch" style="height: auto;" type="text" :selectable="true">
            {{ type === '3' ? item.categoryname : item.categoryName }}
          </text>
        </view>
        <view class="common-list-ex-patch-order">

          <label class="common-list-title-ex">规格 :</label>

          <text class="common-list-text-ex-patch" style="height: auto;" type="text" :selectable="true">
            {{ type === '3' ? item.formpattern : item.formPattern }}
          </text>

        </view>

        <view class="common-list-ex-patch">

          <label class="common-list-title-ex">单位计量 :</label>


          <text class="common-list-text-ex-patch" style="height: auto;" type="text">{{ item.dwjl }}</text>
        </view>
        <view class="common-list-ex-patch">

          <label class="common-list-title-ex">计量单位 :</label>


          <text class="common-list-text-ex-patch" style="height: auto;" type="text">{{ item.unit }}</text>
        </view>

        <view class="common-list-ex-patch">

          <label class="common-list-title-ex">设计数量 :</label>

          <text class="common-list-text-ex-patch" style="height: auto;" type="text">{{
              type == '3' ? prjItem.totalAmount : item.amount
            }}
          </text>
        </view>
        <view class="common-list-ex-patch">

          <label class="common-list-title-ex">累计申领数量 :</label>


          <text class="common-list-text-ex-patch" style="height: auto;" type="text">
            {{ type == '3' ? prjItem.totalSumReceiveAmount : item.sumReceiveAmount }}
          </text>
        </view>
        <view class="common-list-ex-patch">

          <label class="common-list-title-ex">累计出库数量 :</label>


          <text class="common-list-text-ex-patch" style="height: auto;" type="text">
            {{ type == '3' ? prjItem.totalOsRealAmount : item.osRealAmount }}
          </text>
        </view>
        <view class="common-list-ex-patch">

          <label class="common-list-title-ex">可领取数量 :</label>


          <text class="common-list-text-ex-patch" style="height: auto;" type="text">
            {{ type == '3' ? prjItem.totalNoReceiveAmount : item.noReceiveAmount }}
          </text>
        </view>

        <view class="common-list-ex-patch">

          <label class="common-list-title-ex">
            <text class="colorRed">*</text>
            计划领用时间 :</label>

          <!-- <input class="common-list-inpur-ex" type="text" value="选择日期"  /> -->
          <view style="width: 70%;display: flex;background-color: white;margin-top: -6rpx;">
            <!-- 	<uni-datetime-picker type="date"   :start="timestamp"
               :border="false" :clearIcon="false" :value="single"
                    @maskClick="maskClick" v-model="jhlysj" placeholder="请选择相应的日期" /> -->
            <text space="ensp">{{ "  " }}</text>
            <image @click="calendarer" src="/static/imges/rili.png" mode=""
                   style="width: 26rpx;height: 26rpx;margin-top: 25rpx;"></image>
            <text space="ensp">{{ "  " }}</text>
            <u-input style="margin-top: 2rpx" @click="calendarer" v-model="jhlysj" :disabled="true"
                     placeholder="请选择相应的日期"/>
            <u-calendar v-model="show" :mode="mode" @change="change" :min-date="shijan" :max-date="maxshijan"
                        :btn-type="imager" :active-bg-color="yanse"/>
          </view>


        </view>
        <view class="common-list-ex-patch" style="background-color: white;">

          <label class="common-list-title-ex">
            <text class="colorRed">*</text>
            本次申领数量 :</label>

          <input style="height: 40rpx;font-size: 28rpx;background-color: white;margin-top: 10rpx;"
                 class="common-list-inpur-ex" type="digit" v-model="bcslsl"/>

        </view>
        <view class="common-list-ex-patch" v-if="type==='3'">
          <label class="common-list-title-ex">请选择领料方式 :</label>
          <view class="footer-box__item" style="text-align: right;flex: 1">
            <picker @change="changeApplyType" :value="applyTypeIndex" :range="applyTypeArr" range-key="name" style="display: flex;">
              <input class="uni-input" style="width: 450rpx;text-align: left;padding: 4rpx 20rpx"  @focus="aPickerFocus"
                     :value="applyTypeType" type="text">
            </picker>
          </view>
        </view>
        <view class="common-list-ex-patch" style="background-color: rgba(228, 228, 228, 1);height: 120rpx"
              v-if="isNewbusinessCode==='2'&&type!='3'">
          <label class="common-list-title-ex">
            <text class="colorRed">*</text>
            按备电标准化成品申领 :</label>
          <view class="footer-box__item" style="text-align: right" v-if="item.isStand!=='0'">
            <picker @change="changeProductApply" :value="productApplyIndex" :range="productApplyArr" range-key="name" style="display: flex;">
              <input class="uni-input" style="width: 450rpx;text-align: left;height: 120rpx;padding: 0 20rpx"
                     :value="productApplyType" type="text" @focus="aPickerFocus">
            </picker>
          </view>
          <text class="common-list-text-ex-patch" style="height: auto;display: flex;align-items: center" type="text"
                v-if="item.isStand=='0'">否
          </text>
        </view>
      </view>
      <view class="common-body-ex">
        <label class="common-list-title-ex">
          <text class="colorRed">*</text>
          物资申领备注 :</label>
        <view class="common-list-textex-order" style="background-color: #FFFFFF;">
          <textarea v-model="remark" placeholder="请填写备注"
                    style="background-color: #FFFFFF; height: 100px;font-size: 28rpx;"/>
        </view>

      </view>
      <view class="common-btn-body">
        <button class="common-btn" type="warn" v-debounce="()=>save()"
                :disabled="bcslsl == '' || remark == '' || jhlysj == '' || (isNewbusinessCode =='2' && item.isStand =='1'&& productApplyCode=='')">
          提交
        </button>
        <button class="common-btn" @click="chongxiao()" style="border:1px solid #EB4B4B;color:#EB4B4B ;">取消</button>
      </view>
    </view>

  </view>
</template>

<script>
import talkingdata from '../../common/talkingdata.js';

var mThis
export default {
  data() {
    return {
      baseList: [],
      type: '', //单站，批量，整体
      prjInfo: {},
      prjItem: {},
      single: '',
      jhlysj: '',
      bcslsl: '',
      remark: '',
      timestamp: '',
      disabledButton: true,
      show: false,
      mode: 'date',
      shijan: "",
      falseer: false,
      maxshijan: "2123-01-27",
      imager: "error",
      yanse: "#EB4B4B",
      productApplyArr: [
        {
          key: "1",
          name: "是"
        },
        {
          key: "0",
          name: "否"
        },
      ],
      applyTypeArr: [
        {
          key: "1",
          name: "自提"
        },
        {
          key: "2",
          name: "配送"
        },
        {
          key: "3",
          name: "施工单位仓库"
        },
      ],
      productApplyIndex: '',
      productApplyType: '请选择',
      productApplyCode: '',
      applyTypeIndex: '',
      applyTypeType: '请选择',
      applyTypeCode: '',
      isNewbusinessCode: '2',  //'2'能源类项目
    }
  },
  onLoad(option) {
    this.prjItem = JSON.parse(decodeURIComponent(option.prjItem));
    this.prjInfo = JSON.parse(decodeURIComponent(option.prjInfo));
    this.type = option.type
    this.isNewbusinessCode = option.isNewbusinessCode
    this.timestamp = Date.parse(new Date());
    this.baseList.push(JSON.parse(decodeURIComponent(option.item)));
    this.myDate();
  },
  onReady() {
    console.log("执行onReady")
    //在页面挂载完成后，调用外部埋点，统计数据
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = talkingdata.talkingKey;
    document.body.appendChild(script);

  },
  methods: {
    myDate() {
      let date = new Date();

      date.setDate(date.getDate());
      let yue = (date.getMonth() + 1 / 1) > 9 ? (date.getMonth() + 1 / 1) : ("0" + (date.getMonth() + 1 / 1));
      let geate = date.getDate();
      let str = date.getFullYear() + '-' + yue + '-' + geate;
      this.shijan = str;
    },
    changeProductApply(e) {
      this.productApplyIndex = e.detail.value;
      this.productApplyType = this.productApplyArr[e.detail.value].name
      this.productApplyCode = this.productApplyArr[e.detail.value].key
    },
    changeApplyType(e) {
      this.applyTypeIndex = e.detail.value;
      this.applyTypeType = this.applyTypeArr[e.detail.value].name
      this.applyTypeCode = this.applyTypeArr[e.detail.value].key
      if(this.applyTypeArr[e.detail.value].key==='1'){
        this.remark='物料自提，无需送货'
      }else if(this.applyTypeArr[e.detail.value].key==='2'){
        this.remark='物料配送地址：'
      }else if(this.applyTypeArr[e.detail.value].key==='3'){
        this.remark=''
      }else{
          console.log('啥也不是')
      }
    },
    change(e) {
      this.jhlysj = e.result
    },
    calendarer() {
      this.show = true;
    },
    aPickerFocus() {
      uni.hideKeyboard()
    },
    back() {
      uni.navigateBack({
        delta: 1
      });
    },
    maskClick(e) {
      console.log("出发==" + e)

    },
    chongxiao() {
      this.jhlysj = '';
      this.bcslsl = '';
      this.remark = '';
      uni.navigateBack({
      		delta: 1
      })
    },

    confirmRequest(url, params) {
      this.$axios.post(url, params).then((result) => {
        uni.showModal({
          title: '温馨提示',
          content: result.message,
          showCancel: false,
          confirmColor: '#eb4b4b',
          success: function (res) {
            if (res.confirm) {
              //该方法用于刷新父页面
              uni.navigateBack({
                delta: 1
              })
            }
          }
        })
      })
    },
    getMaterialCode(params){
      let that=this;
      let paramsCode = {
        materialCode: that.type === '3' ? that.baseList[0].categorycode : that.baseList[0].categoryCode,
        receiveAmount: that.bcslsl,
        amountType: '1',
        siteId: that.baseList[0].siteId,
      }
      //精度校验
      that.$axios.post('/material/api/receive/equip/getSpuUnitAccByMaterialCode', paramsCode).then(() => {
        if (that.type !== '3') {
          that.confirmRequest("/material/api/receive/equip/doSaveApplyBySite", params);
        } else {
          that.confirmRequest("/material/api/receive/equip/addApplyDetail", params);
        }
      })
    },

    save() {
      let that = this
      if (that.jhlysj == "") {
        uni.showModal({
          title: '温馨提示',
          content: "请输入计划领用时间",
          showCancel: false,
          confirmColor: '#eb4b4b',
        })
        return;
      }
      if (that.bcslsl == "") {
        uni.showModal({
          title: '温馨提示',
          content: "请输入本次申领数量",
          showCancel: false,
          confirmColor: '#eb4b4b',
        })
        return;
      }
      if (that.remark == "") {
        uni.showModal({
          title: '温馨提示',
          content: "请输入申领备注",
          showCancel: false,
          confirmColor: '#eb4b4b',
        })
        return;
      }
      if(that.type !== '3' && that.isNewbusinessCode ==='2' && that.baseList[0].isStand ==='1'&& that.productApplyCode===''){
        uni.showModal({
          title: '温馨提示',
          content: "请选择按备电标准化成品申领",
          showCancel: false,
          confirmColor: '#eb4b4b',
        })
        return;
      }

      //传参这块应该分单站批量项目还是整体项目
      let params = {
        prjInfo: {
          adjId: that.prjInfo.adjId,
          applyType: that.type === '3' && that.applyTypeCode ? that.applyTypeCode : "1",
          note: that.remark,
          planTime: that.jhlysj,
          prjId: that.prjInfo.prjId,
        },
        applys: [
          {
            ...that.baseList[0],
            receiveAmount: that.bcslsl,
          }
        ]
      }
      delete params.applys[0].productApplyIndex
      delete params.applys[0].selectedCode
      delete params.applys[0].selectedType
      if (that.type === '3') {
        params.applys[0].categoryCode = params.applys[0].categorycode;
        params.applys[0].categoryName = params.applys[0].categoryname;
        params.applys[0].ifSourcecode = params.applys[0].ifSourceCode;
        params.applys[0].prjCode = params.applys[0].prjcode;
        params.applys[0].prjId = params.applys[0].prjid;
        params.applys[0].oldPrice = params.applys[0].oldprice;
        delete params.applys[0].categorycode;
        delete params.applys[0].categoryname;
        delete params.applys[0].ifSourceCode;
        delete params.applys[0].prjcode;
        delete params.applys[0].prjid;
        delete params.applys[0].formpattern;
        delete params.applys[0].oldprice;
      } else {
        params.applys[0].isStandValue = that.baseList[0].isStand !== '0' ? that.productApplyCode : that.baseList[0].isStandValue
      }
      console.log("参数", params)

      uni.showModal({
        title: '温馨提示',
        content: "是否确认提交",
        confirmColor: '#eb4b4b',

        success: function (res1) {
          if (res1.confirm) {
            that.getMaterialCode(params);
          }
        }
      });

    },


  }
}
</script>


<style>
@import '@/common/common-ui.css';
</style>
